﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NNote.Properties;
using System.IO;

namespace NNote {
	public partial class MainForm : Form {
		public MainForm() {
			InitializeComponent();
		}

		public void OpenFile(string filename) {
			textBox1.LoadFile(filename);
			SetTitle();
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);
			BindSettings();
			if (textBox1.FilePath == null) {
				textBox1.Modified = false;
				command_new(null, EventArgs.Empty);
			}
		}

		private void BindSettings() {
			textBox1.Font = Settings.Default.TextFont ?? SystemFonts.DefaultFont;
			textBox1.BackColor = Settings.Default.BackColor ?? SystemColors.Window;
			textBox1.ForeColor = Settings.Default.TextColor ?? SystemColors.WindowText;
			textBox1.WordWrap = Settings.Default.WordWrap;
		}

		private void command_showoptions(object sender, EventArgs e) {
			var result = new OptionsDialog().ShowDialog(this);
			if (result == DialogResult.OK)
				BindSettings();
		}

		private void command_new(object sender, EventArgs e) {
			if (CheckShouldSave()) {
				textBox1.NewFile();
				SetTitle();
			}
		}

		private void command_open(object sender, EventArgs e) {
			if (CheckShouldSave()) {
				var result = openFileDialog1.ShowDialog();
				if (result == DialogResult.OK) {
					OpenFile(openFileDialog1.FileName);
				}
			}
		}

		private void command_save(object sender, EventArgs e) {
			if (textBox1.FilePath == null) {
				command_saveas(sender, e);
			} else {
				textBox1.SaveFile(textBox1.FilePath);
				SetTitle();
			}
		}

		private void command_saveas(object sender, EventArgs e) {
			if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
				textBox1.SaveFile(saveFileDialog1.FileName);
				SetTitle();
			}
		}
		private void command_cut(object sender, EventArgs e) {
			textBox1.Cut();
		}
		private void command_copy(object sender, EventArgs e) {
			textBox1.Copy();
		}
		private void command_paste(object sender, EventArgs e) {
			textBox1.Paste();
		}
		private void command_selectAll(object sender, EventArgs e) {
			textBox1.SelectAll();
		}
		private bool CheckShouldSave() {
			if (textBox1.Modified) {
				var result = new CloseFileDialog(textBox1.FileTitle)
					.ShowDialog(this);
				switch (result) {
					case DialogResult.Yes:
						command_save(null, EventArgs.Empty);
						return true;
					case DialogResult.No:
						return true;
					case DialogResult.Cancel:
						return false;
					default:
						throw new NotSupportedException();
				}
			} else {
				return true;
			}
		}

		private void SetTitle() {
			this.Text = string.Format(
				"{0}{1} - NNote",
				textBox1.FileTitle,
				textBox1.Modified ? "*" : ""
			);
		}

		private void textBox1_ModifiedChanged(object sender, EventArgs e) {
			SetTitle();
		}

		private void command_setWordWrap(object sender, EventArgs e) {
			textBox1.WordWrap = wordwrapMenuItem.Checked;
		}

		private void command_helpAbout(object sender, EventArgs e) {
			new AboutBox().ShowDialog(this);
		}
	}
}
